package org.me.runaway;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
import java.util.ArrayList;


public class MapItemizedOverlay extends ItemizedOverlay {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    Activity mContext;
    Intent details;

    public MapItemizedOverlay(Drawable defaultMarker, Activity context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    @Override
    protected boolean onTap(int index) {


        OverlayItem item = mOverlays.get(index);
        if (!item.getTitle().equals("Localização Actual")) {
            showDetails(item.getTitle());
        } else {
            AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
            dialog.setMessage(item.getTitle());
            dialog.setPositiveButton("Back", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int id) {
                }
            });
            dialog.show();
        }


        return true;
    }

    private void showDetails(String name) {
        DB_Adapter db = new DB_Adapter(mContext);
        db.open();
        Cursor cs = db.querySearch(name);


        if (cs != null) {
            cs.moveToFirst();
            details = new Intent(mContext, PlaceDetails.class);
            details.putExtra(Place.KEY_ROWID, cs.getLong(cs.getColumnIndex(Place.KEY_ROWID)));
            details.putExtra(Place.KEY_NAME, cs.getString(cs.getColumnIndex(Place.KEY_NAME)));
            details.putExtra(Place.KEY_ADDRESS, cs.getString(cs.getColumnIndex(Place.KEY_ADDRESS)));
            details.putExtra(Place.KEY_TYPE, cs.getString(cs.getColumnIndex(Place.KEY_TYPE)));
            details.putExtra(Place.KEY_CONTACT1, cs.getString(cs.getColumnIndex(Place.KEY_CONTACT1)));
            details.putExtra(Place.KEY_CONTACT2, cs.getString(cs.getColumnIndex(Place.KEY_CONTACT2)));
            details.putExtra(Place.KEY_LAT, cs.getString(cs.getColumnIndex(Place.KEY_LAT)));
            details.putExtra(Place.KEY_LNG, cs.getString(cs.getColumnIndex(Place.KEY_LNG)));
            details.putExtra(Place.KEY_DETAILS, cs.getString(cs.getColumnIndex(Place.KEY_DETAILS)));
            details.putExtra(Place.KEY_PRICE, cs.getString(cs.getColumnIndex(Place.KEY_PRICE)));
            details.putExtra(Place.KEY_SCHEDULE, cs.getString(cs.getColumnIndex(Place.KEY_SCHEDULE)));
            details.putExtra(Place.KEY_RATE, cs.getString(cs.getColumnIndex(Place.KEY_RATE)));
            details.putExtra(Place.KEY_TYPE, cs.getString(cs.getColumnIndex(Place.KEY_TYPE)));
            details.putExtra(Place.KEY_IMAGE, cs.getString(cs.getColumnIndex(Place.KEY_IMAGE)));
            details.putExtra(Place.KEY_FAVORITE, cs.getString(cs.getColumnIndex(Place.KEY_FAVORITE)));
            details.putExtra(Place.KEY_VOTED, cs.getString(cs.getColumnIndex(Place.KEY_VOTED)));
            mContext.startActivity(details);
            mContext.finish();
        }
        db.close();

    }
}
